<?php

declare(strict_types=1);

namespace app\common\model;

use addons\Target\facade\Target;

class UserSign extends Base
{

    protected $name = 'user_sign';
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile');
    }
    /**
     * 添加抽奖记录
     */
    public static function userSignAdd($user,$sign_id)
    {
        parent::startTrans();
        try {
            $find = SignInfo::where(['id'=>$sign_id])->find();
            if(empty($find)){
                return ['code' => -1, 'msg' => '系统错误请重新签到'];
            }
            $data['sign_id'] = $sign_id;
            $data['lotto'] = $find['jiangli'];
            $data['uid'] = $user->id;
            parent::create($data);
            $user->lotto= $find['jiangli'];
            $user->save();
            Relation::freeBonus($user,$find['jiangli']);
            parent::commit();
            return ['code' => 1, 'msg' => '签到成功！获得'.$find['title']];
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
